      <div class="templatemo-content-wrapper">
        <div class="templatemo-content">
          <ol class="breadcrumb">
            <li><a href="<?=site_url()?>/order/index">订单管理</a></li>
            <li class="active">订单详情</li>
          </ol>
          <div class="row">
          	<div class="col-md-12">
            	<button class="btn btn-primary" id="remindpay_btn">提醒支付</button>
            	<button class="btn btn-primary" id="remindtime_btn">提醒下单时间</button>
            </div>
            <div class="col-md-12">
             <!-- <div class="btn-group pull-right" id="templatemo_sort_btn">
                <button type="button" class="btn btn-default">Sort by</button>
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                  <span class="caret"></span>
                  <span class="sr-only">Toggle Dropdown</span>
                </button>
                <ul class="dropdown-menu" role="menu">
                  <li><a href="#">First Name</a></li>
                  <li><a href="#">Last Name</a></li>
                  <li><a href="#">Username</a></li>
                </ul>
             </div>-->
              
              <div class="table-responsive">
                <h4 class="margin-bottom-15">订单详情</h4>
                <table class="table table-striped table-hover table-bordered">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>操作</th>
                      <th >店名</th>
                      <th>用户</th>
                      <th>下单时间</th>
                      <th>支付方式</th>
                      <th>支付时间</th>
                      <th>收货时间</th>
                       <th>备注</th>
                      <th>状态</th>
                      <th>订单编号</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td><?php echo $order['id'];?>
                      </td>
                      <td>
                           <?php 
                        if($order['state'] == 1 || ($order['state'] == 0&&$order['payment_mode']==3) )
                        {
                        	echo '<a href="#" class="btn btn-link check" data-id="'.$order['id'].'" >确认订单</a>';
						}
						 if($order['state'] == 2)
                        {
                        	echo '<a href="#" class="btn btn-link send" data-id="'.$order['id'].'" >开始配送</a>';
						}		
                        if($order['state'] == 3 || $order['state'] == 5)
                        {
                        	echo '<a href="#" class="btn btn-link finish" data-id="'.$order['id'].'" mode="'.$order['payment_mode'].'" >完成订单</a>';
						}
						if($order['state'] == 3&&$order['payment_mode']==3)
                        {
                        	echo '<a href="#" class="btn btn-link unpay" data-id="'.$order['id'].'"  >未结算</a>';
						}		
						?>
					  </td>
                      <td><?php echo $order['shopname'];?></td>
                      <td><a href="<?php echo site_url('user/index/name/'.$order['userid'])?>" class="btn btn-link"><?php echo $order['name'];?></a></td>
                      <td><?php echo $order['order_time'];?></td>
                      <td>
                      	  <select id="payment">
                      	  	   <option value="0" <?php if($order['payment_mode'] ==0) echo "selected"; ?>>空</option>
            				   <option value="1" <?php if($order['payment_mode'] ==1) echo "selected"; ?>>支付宝支付</option>
            				   <option value="2" <?php if($order['payment_mode'] ==2) echo "selected"; ?>>微信支付</option>
            				   <option value="3" <?php if($order['payment_mode'] ==3) echo "selected"; ?>>货到付款</option>
            		    	   <option value="4" <?php if($order['payment_mode'] ==4) echo "selected"; ?>>余额支付</option>
            			  </select>
            			  <button class="change" data-id="<?php echo $order['id'];?>">确认</button>
                    </td>
                    <td><?php echo $order['payment_time'];?></td>
                     <td>
                     	 <input style="width:80px" type="text"  id="receive" value="<?php echo date("Y-m-d",strtotime($order['receive_time']));?>">
                      	 <button class="receive" data-id="<?php echo $order['id'];?>" >更新</button>
                     </td>
                      <td><?php echo $order['note'];?></td>
                       <td><?php 
                      	if($order['state'] == -1&&$order['merge'] ==0)
                        {
                        	echo "已取消";
                        }
						if($order['state'] == -1&&$order['merge'] ==1)
                        {
                        	echo "已合并";
                        }
                        if($order['state'] == 0)
                        {
                        	echo "待支付";
                        }
						if($order['state'] == 1)
                        {
                        	echo "已支付";
                        }
						if($order['state'] == 2)
                        {
                        	echo "拣货中";
                        }
						if($order['state'] == 3)
                        {
                        	echo "配送中";
                        }
						if($order['state'] == 4)
                        {
                        	echo "订单完成";
                        }
						if($order['state'] == 5)
                        {
                        	echo "已配送未支付";
                        }
                        ?></td>
                        <td><?php echo $order['orderno'];?></td>
                    </tr>
                  </tbody>
                </table>
              </div>
               <div class="table-responsive">
                <h4 class="margin-bottom-15">商品明细</h4>
                <input type="text" id="neworderid" name="neworderid" value="<?php echo $neworderid;?>">
            	<button class="btn btn-primary" id="copy_btn">推送</button>
                <table class="table table-striped table-hover table-bordered">
                  <thead>
                    <tr>
                      <th>ID</th>
                      <th>商品</th>
                      <th>数量</th>
                      <th>单位</th>
                      <th>单价</th>
                      <th>合计</th>
                      <th>操作</th>
                    </tr>
                  </thead>
                  <tbody>
                  	 <?php 
                  	 foreach ($orderdetails as $item):?>
                    <tr>
                      <td><?php echo $item['id'];?></td>
                      <td><?php echo $item['name'];?></td>
                      <td>
                      		<input style="width:40px" type="text" id="sum<?php echo $item['id'];?>" value="<?php echo $item['amount'];?>">
                      	 	<button class="update" data-id="<?php echo $item['id'];?>" >更新</button>
                      </td>
                      <td><?php echo $item['unit'];?></td>
                      <td><?php echo $item['price'];?></td>
                      <td><?php echo $item['amount']*$item['price'];?>元</td>
                      <td> 
                      		<a href="#" class="btn btn-link del" data-id="<?php echo $item['id'];?>" >删除</a>
                      </td>
                    </tr>
                    <?php
                     endforeach;?>
                     <?php if(!empty($coupondetail)){ ?>
                     	<td>优惠券</td>
                     	<td>
                     		满<?php echo $coupondetail['more'];?>减<?php echo $coupondetail['off'];?>元
                     		<a href="#" class="btn btn-link" id="delcou">删除</a>
                     	</td>
                     <?php } ?>
                    <tr>
                     	<td>总计</td>
                     	<td><?php 
                     	if(!empty($coupondetail)){
                     		echo $order['total']-$coupondetail['off'];
                     	}else{
							echo $order['total'];
						}
                     	?>元</td>
                    </tr>
                  </tbody>
                </table>
              </div>
              <div class="col-md-12">
              		<form role="form" id="templatemo-preferences-form"
						onsubmit="return false;">
             		 	<div class="row">
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">商品名称</label> <input
									type="text" class="form-control" id="name" value="">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="lastName" class="control-label">销售价</label> <input
									type="text" class="form-control" id="price" value="">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="firstName" class="control-label">数量</label> <input
									type="text" class="form-control" id="amount" value="">
							</div>
							<div class="col-md-6 margin-bottom-15">
								<label for="lastName" class="control-label">单位</label> <input
									type="text" class="form-control" id="unit" value="">
							</div>
						</div>
						<div class="row templatemo-form-buttons">
							<div class="col-md-12">
								<input type="hidden" id="orderid" value="<?=$order['id']?>" />
								<input type="hidden" id="userid" value="<?=$order['userid']?>" />
								<button type="submit" class="btn btn-primary" id="submit_btn">添加</button>
							</div>
						</div>
					</form>
			</div>
            </div>
          </div>
        </div>
      </div>
    <script>
	$(document).ready(function() {
		$(".check").click(function() {
			if(confirm("确定确认该订单吗？"))
			{
			var id = $(this).attr("data-id");
			$.ajax({
				url : '<?php echo site_url('order/edit')?>',
				type : "post",
				async : true,
				data : {
					'id' : id,
					'state':2
				},
				dataType : "json",
				timeout : "1000",
				error : function() {
					alert("服务加载出错");
				},
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
		$(".send").click(function() {
			if(confirm("确定配送该订单吗？"))
			{
			var id = $(this).attr("data-id");
			$.ajax({
				url : '<?php echo site_url('order/edit')?>',
				type : "post",
				async : true,
				data : {
					'id' : id,
					'state':3
				},
				dataType : "json",
				timeout : "1000",
				error : function() {
					alert("服务加载出错");
				},
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
		$(".finish").click(function() {
			if(confirm("确定完成该订单吗？"))
			{
			var id = $(this).attr("data-id");
			$.ajax({
				url : '<?php echo site_url('order/edit')?>',
				type : "post",
				async : true,
				data : {
					'id' : id,
					'state':4
				},
				dataType : "json",
				timeout : "1000",
				error : function() {
					alert("服务加载出错");
				},
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
		$(".unpay").click(function() {
			if(confirm("确定该订单未结算吗？"))
			{
			var id = $(this).attr("data-id");
			$.ajax({
				url : '<?php echo site_url('order/edit')?>',
				type : "post",
				async : true,
				data : {
					'id' : id,
					'state':5
				},
				dataType : "json",
				timeout : "1000",
				error : function() {
					alert("服务加载出错");
				},
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
		$("#name").autocomplete({
			minLength:1,
			source:function(request,response){
				var userid = $("#userid").val();
       			 $.ajax({
                    url:'<?php echo site_url('product/getShowProducts')?>',
                    type : "post",
                    dataType: "json",
                    data:{
                        name: request.term,
                        userid: userid
                    },
                    success: function( data ) {
                        response($.map(data, function(item) {
    		            return {
    		                 label: item.name+'-'+item.description,
    		                 value: item.name+ '-' + item.id,
    		                 price:item.price,
    		                 unit:item.unit
    		             }
    		         }));
                    }
                });
			},
			select: function( event, ui ) {
        		$( "#price" ).val( ui.item.price );
        		$( "#unit" ).val( ui.item.unit );
      		}
		});
		$("#submit_btn").click(function() {
			var orderid = $("#orderid").val();
			var name = $("#name").val();
			var price = $("#price").val();
			var amount = $("#amount").val();
			$.ajax({
				url : '<?php echo site_url('order/addorderdetail')?>',
				type : "post",
				async : true,
				data : {
					'orderid' : orderid,
					'name' : name,
					'price' : price,
					'amount' : amount
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
		});
		$("#remindpay_btn").click(function() {
			if(confirm("确定提醒该订单吗？"))
			{
			var orderid = $("#orderid").val();
			$.ajax({
				url : '<?php echo site_url('order/remindPay')?>',
				type : "post",
				async : true,
				data : {
					'orderid' : orderid
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
		$("#remindtime_btn").click(function() {
			if(confirm("确定提醒该订单吗？"))
			{
			var orderid = $("#orderid").val();
			$.ajax({
				url : '<?php echo site_url('order/remindTime')?>',
				type : "post",
				async : true,
				data : {
					'orderid' : orderid
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
		$(".receive").click(function() {
			var orderid = $(this).attr("data-id");
			var receive = $("#receive").val();
			$.ajax({
				url : '<?php echo site_url('order/updatereceive')?>',
				type : "post",
				async : false,
				data : {
					'orderid' : orderid,
					'receive' : receive
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						//window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
		});
		$(".change").click(function() {
			var orderid = $(this).attr("data-id");
			var payment = $("#payment").val();
			$.ajax({
				url : '<?php echo site_url('order/updatepayment')?>',
				type : "post",
				async : false,
				data : {
					'orderid' : orderid,
					'payment' : payment
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						//window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
		});
		$(".update").click(function() {
			var orderid = $("#orderid").val();
			var orderdetailid = $(this).attr("data-id");
			var amount = $("#sum"+orderdetailid).val();
			$.ajax({
				url : '<?php echo site_url('order/updateorderdetail')?>',
				type : "post",
				async : false,
				data : {
					'orderid' : orderid,
					'orderdetailid' : orderdetailid,
					'amount':amount
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
		});
		$(".del").click(function() {
			if(confirm("确定删除该订单详情吗？"))
			{
			var orderid = $("#orderid").val();
			var orderdetailid = $(this).attr("data-id");
			$.ajax({
				url : '<?php echo site_url('order/deleteorderdetail')?>',
				type : "post",
				async : true,
				data : {
					'orderid' : orderid,
					'orderdetailid' : orderdetailid
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
		$("#delcou").click(function() {
			if(confirm("确定删除该优惠券吗？"))
			{
			var orderid = $("#orderid").val();
			$.ajax({
				url : '<?php echo site_url('order/deletecoupon')?>',
				type : "post",
				async : true,
				data : {
					'orderid' : orderid
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
						window.location.reload();
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
		$("#copy_btn").click(function() {
			if(confirm("确定推送到该订单吗？"))
			{
			var orderid = $("#orderid").val();
			var neworderid = $("#neworderid").val();
			$.ajax({
				url : '<?php echo site_url('order/pushorderdetail')?>',
				type : "post",
				async : true,
				data : {
					'orderid' : orderid,
					'neworderid' : neworderid
				},
				dataType : "json",
				success : function(data) {
					if(data.code)
					{
						alert(data.msg);
					}else{
						alert(data.msg);
					}
				}
			});
			}
		});
	}); 
</script>